package com.cm.projectx.controller.search;

import com.cm.projectx.entity.PriceTask;
import com.cm.projectx.response.Result;
import com.cm.projectx.service.IPriceTaskService;
import com.cm.projectx.service.IProductPriceReportDetailService;
import com.cm.projectx.service.ISubEditabletable1756697848650FdmikchnService;
import com.cm.projectx.util.PoiHelper;
import com.cm.projectx.vo.PriceSearchVo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v2api/price/search")
@AllArgsConstructor
public class PriceSearchController {
    private final ISubEditabletable1756697848650FdmikchnService subEditableTable1756697848650FdmikchnService;
    private final IProductPriceReportDetailService productPriceReportDetailService;
    private final IPriceTaskService priceTaskService;

    /**
     * 查询
     * @param priceSearchVo
     * @return
     */
    @GetMapping("/page")
    @ResponseBody
    public List<Map<String, Object>> searchPrice(PriceSearchVo priceSearchVo) {
        return subEditableTable1756697848650FdmikchnService.selectPage(priceSearchVo);
    }

    /**
     * 查询
     * @param priceSearchVo
     * @return
     */
    @GetMapping("/report")
    @ResponseBody
    public Result searchPriceReport(PriceSearchVo priceSearchVo) {
        Result result = new Result();
        result.addData(productPriceReportDetailService.selectPage(priceSearchVo));
        return result;
    }

    /**
     * 查询
     * @param priceSearchVo
     * @return
     */
    @GetMapping("/report/v3")
    @ResponseBody
    public Result searchPriceReportV3(PriceSearchVo priceSearchVo) {
        Result result = new Result();
        PriceTask priceTask = priceTaskService.getById(priceSearchVo.getTaskId());
        if (priceTask == null) {
            return result;
        }

        result.addData(productPriceReportDetailService.selectPageV3(priceSearchVo,priceTask));
        result.addData("reportType",priceTask.getDkTemplateTypesCode());
        return result;
    }

    /**
     * 查询地区或监测点列表
     * @param type
     * @param district
     * @return
     */
    @GetMapping("/monitor/point")
    @ResponseBody
    public Result monitorPointSearch(String type, String district) {
        Result result = new Result();
        result.addData("data", productPriceReportDetailService.selectMonitorPoints(type, district));
        return result;
    }

    /**
     * 查询
     * @param priceSearchVo
     * @return
     */
    @GetMapping("/date/list")
    @ResponseBody
    public List<String> getDateList(PriceSearchVo priceSearchVo) {
        return subEditableTable1756697848650FdmikchnService.getDateList(priceSearchVo);
    }

    /**
     * 查询
     * @param priceSearchVo
     * @return
     */
    @GetMapping("/product/type")
    @ResponseBody
    public Result getProductTypes(PriceSearchVo priceSearchVo) {
        return productPriceReportDetailService.getProductTypes();
    }

    /**
     * 查询
     * @param priceSearchVo
     * @return
     */
    @GetMapping("/date/list/v2")
    @ResponseBody
    public Result getDatePickerList(PriceSearchVo priceSearchVo) {
        return productPriceReportDetailService.getDateList(priceSearchVo);
    }

    /**
     * 查询
     * @param priceSearchVo
     * @return
     */
    @GetMapping("/date/list/v3")
    @ResponseBody
    public Result getDatePickerListV3(PriceSearchVo priceSearchVo) {
        return productPriceReportDetailService.getDateListV3(priceSearchVo);
    }

    /**
     * 查询
     * @param priceSearchVo
     * @return
     */
    @GetMapping("/task/list/v2")
    @ResponseBody
    public Result getTaskList(PriceSearchVo priceSearchVo) {
        return productPriceReportDetailService.getTaskList(priceSearchVo);
    }

    /**
     * 根据条件导出文件
     *
     * @param response
     */
    @PostMapping(value = "file", produces = {"application/octet-stream"})
    public void exportExcel(HttpServletResponse response, PriceSearchVo priceSearchVo) {
        Map<String, Object> fileData = subEditableTable1756697848650FdmikchnService.exportExcel(priceSearchVo);
        PoiHelper.downFileByBrowser(fileData, response);
    }

    /**
     * 根据条件导出文件
     *
     * @param response
     */
    @PostMapping(value = "file/v2", produces = {"application/octet-stream"})
    public void exportExcelV2(HttpServletResponse response, @RequestBody PriceSearchVo priceSearchVo) {
        Map<String, Object> fileData = productPriceReportDetailService.exportExcel(priceSearchVo);
        PoiHelper.downFileByBrowser(fileData, response);
    }

    /**
     * 根据条件导出文件
     *
     * @param response
     */
    @PostMapping(value = "file/v3", produces = {"application/octet-stream"})
    public void exportExcelV3(HttpServletResponse response, @RequestBody PriceSearchVo priceSearchVo) {
        PriceTask priceTask = priceTaskService.getById(priceSearchVo.getTaskId());
        if (priceTask == null) {
            return ;
        }
        Map<String, Object> fileData = productPriceReportDetailService.exportExcelV3(priceSearchVo, priceTask);
        PoiHelper.downFileByBrowser(fileData, response);
    }
}
